<template>
    <view class="page-wrap">

        <view class="tabs">
            <view class="tab" :class="{ active: current == 1 }" @tap="changeCurrent('1')">
                <text>已完成({{ p_num }})</text>
                <view v-if="current == 1" class="icon"></view>
            </view>
            <view class="tab" :class="{ active: current == 2 }" @tap="changeCurrent('2')">
                <text>未完成({{ n_num }})</text>
                <view v-if="current == 2" class="icon"></view>
            </view>
        </view>
        <view class="searchForm">
            <uni-data-picker :localdata="regionOptions" v-model="rid" popup-title="请选择区域" :map="map" @change="onchange"
                @nodeclick="onnodeclick"></uni-data-picker>
            <input type="text" v-model="vague" class="address" />
            <view class="btn" @tap="init">
                查询
            </view>
        </view>
        <view class="list-data-box">
            <view class="list-data" v-for="(item, index) in works" :key="index">
                <view class="baseInfo">
                    <view class="address" @tap="jump('/pages/information/information?id=' + item.id)">
                        <view class="label">{{ item.name }}</view>
                        <view class="poi" v-if="item.km">
                            <image class="icon" src="/static/dingwei_icon1.png"></image>
                            <view class="txt">{{ item.km }}</view>
                        </view>
                    </view>
                    <view class="address-detail">
                        {{ item.address }}
                    </view>
                    <view class="daohan">
                        <view class="type">
                            <view class="label">类型：</view>
                            <view class="txt">{{ userWokTypeMap[item.type] }}</view>
                        </view>
                        <view class="btn" @tap="getGPS(item.lat, item.lon)">导航去</view>
                    </view>

                </view>
                <view class="job-total">
                    <view class="item" v-for="(citem, cindex) in jobMap" :key="cindex"
                        @tap="openAlert(item,  citem.key)">
                        <view class="name">{{ citem.label }}</view>
                        <view class="num">{{ getNum(citem.value, citem.type, item.num) }}{{ current == 2 ? '%' : '' }}
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <PopView :title="currentJobType" :popShow="popShow" @popClose="popClose" @popOpen="popOpen">
            <template slot="content">
                <view class="poplist pop-content">
                    <view class="item" v-for="(item, index) in currentJobList" :key="index" @tap="toJob(item)">
                        <view class="item-job">
                            <text style="z-index: 10;position: relative;">{{ item.updated_at || item.created_at }}</text>
                            <text class="item-job-bj"></text>
                        </view>
                    </view>
                </view>
            </template>
        </PopView>
    </view>
</template>

<script>
import { workerList, getMemberRegion, getMonadList,getCanWork } from "@/api"
export default {
    computed: {

    },
    data() {
        return {
            popShow: false,
            map: { text: 'name', value: 'id' },
            title: '',
            currentPath: '',
            currentJobType: "",
            currentJobList: "",
            regionOptions: [],
            works: [],
            value: '',
            current: 1,
            vague: '',
            rid: '',
            show: false,
            customItem: '',
            jobMap: [
                {
                    value: '0',
                    label: '巡检',
                    path: `/pages/patrol/patrol`,
                    type: '1',
                    key:'patrol_id'
                },
                {
                    value: '0',
                    label: '维修',
                    path: '/pages/fix/fix',
                    type: '2',
                    key:'maintain_id'
                },
                {
                    value: '0',
                    label: '隐患',
                    type: '3',
                    path: '/pages/problem/problem',
                    key:'pitfall_id'
                },
                {
                    value: '0',
                    label: '培训',
                    type: '4',
                    path: '/pages/training/training',
                    key:'train_id'
                },
                {
                    value: '0',
                    label: '拆迁',
                    type: '5',
                    path: '/pages/move/move',
                    key:'migrate_id'
                },
                {
                    value: '0',
                    label: '测试',
                    type: '6',
                    path: '/pages/test/test',
                    key:'test_id'
                },
            ],
            // `type` 客户类型 1.网点 2.基站 3.安防',
            userWokTypeMap: {
                1: '网点',
                2: '基站',
                3: '安防'
            },
            p_num: 0,
            n_num: 0
        }
    },
    onLoad() {
        this.getMemberRegion()
        this.init()
    },
    onShow(){
        this.getMemberRegion()
        this.init()
    },
    methods: {
        async openAlert(item, key) {
			this.currentWork = item
			let params = {
				pid: item.pid,
				wid: this.wid,
				mid: item.id,
				type: item.type
			}
			let res = await getCanWork(params)
			let arr = null;
			// 巡检单-网点
			if (res.patrol_state == 1&&key=='patrol_id') {
				//  if (true) {
				arr={
					label: '巡检单',
					path: `/pages/patrol/patrol?type=${item.type}&id=${res.patrol_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				}

			}
			// 测试
			if (res.test_state == 1 && key=='test_id') {
				// if (true) {
				arr={
					label: '测试单',
					path: `/pages/test/test?type=${item.type}&id=${res.test_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				}
			}
			// 培训
			if (res.train_state == 1 && key=='train_id') {
				// if (true) {
				arr={
					label: '培训单',
					path: `/pages/training/training?type=${item.type}&id=${res.train_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				}
			}
			// 迁拆
			if (res.migrate_state == 1 && key=='migrate_id') {
				// if (true) {
				arr={
					label: '迁拆单',
					path: `/pages/move/move?type=${item.type}&id=${res.migrate_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				}
			}

			// 维修
			if (res.maintain_state == 1 && key=='maintain_id') {
				// if (true) {
				arr={
					label: '维修单',
					path: `/pages/fix/fix?type=${item.type}&id=${res.maintain_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				}
			}

			// 隐患
			if (res.pitfall_state == 1 && key=='pitfall_id') {
				// if (true) {
				arr={
					label: '隐患单',
					path: `/pages/problem/problem?type=${item.type}&id=${res.pitfall_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				}
			}

            if(arr){
                this.jump(arr.path)
                return 
            }

            return wx.showToast({
                title:'没有该类型的工单任务',
                icon:'none'
            })

		},
        toJob(item) {
            console.log(this.currentPath)
            this.jump(this.currentPath + `?type=${item.type}&id=${item.id}&mid=${item.mid}&pid=${item.pid}&isDetail=''`, () => {
                this.popShow = false
            })
        },
        async getMonadList(pid, type, label, path) {
            this.currentJobType = label
            this.currentPath = path
            let res = await getMonadList({
                wid: this.wid,
                type
            })
            console.log(res, 1111)
            this.currentJobList = res || []
            this.popShow = true
        },
        onchange(e) {
            const value = e.detail.value
            console.log(value, this.rid)
        },
        onnodeclick(node) {
        },
        init() {
            this.works = []
            this.p_num = 0,
                this.n_num = 0
            wx.showLoading({
                title: '加载中',
                mask: true
            })
            Promise.all([
                this.workerList(),
            ]).finally(() => {
                setTimeout(() => {
                    wx.hideLoading()
                }, 1000)
            })
        },
        changeCurrent(type) {
            this.current = type
            setTimeout(() => {
                this.init()
            })
        },
        async workerList() {
            let res = await workerList({
                wid: this.wid,
                type: this.current,
                rid: this.rid,
                vague: this.vague
            })
            this.p_num = res.p_num
            this.n_num = res.n_num
            let arr = []
            Object.keys(res.list).forEach((key) => {
                let item = res.list[key]
                arr.push(item)
            })
            let latitude = wx.getStorageSync("latitude");
            let longitude = wx.getStorageSync("longitude");
            this.works = arr.map(v => {
				return {
					...v,
					km: this.getDistance(latitude, longitude, v.lat, v.lon),
					distance:this.getDistance2(latitude, longitude, v.lat, v.lon),
				}
			}).sort((a,b)=>a.distance-b.distance)

			console.log(this.works,222)
        },
        getNum(old, type, map) {
            if (!map[type]) return old
            return map[type]
        },
        async getMemberRegion() {
            let res = await getMemberRegion({
                mid: '',
                wid: this.wid
            })
            this.regionOptions = this.toTree(res)
            console.log(this.regionOptions, 3333)
        },
    }
}
</script>

<style src="./assets/index.scss" lang="scss" scoped></style>


